栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springcloud-Eureka注册与服务创建(单群)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springcloud-Eureka注册与服务创建(单群)

1.父工程pom.xml如下:



    4.0.0

    org.example
    spring boot-demon2
    pom
    1.0-SNAPSHOT
    
        eureka-instance
        eureka-center
        eurekaClient
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.3
    
    

        
            org.springframework.boot
            spring-boot-devtools
            true
        

        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                2020.0.1
                pom
                import
            
        
    
    
        
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                    common.startMain
                
            
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

2.注册中心

(1)pom.xml如下:



    
        spring boot-demon2
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    eureka-center
    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-security
        
    

(2)配置文件如下(application.yml)

spring:
  application:
    name: eureka-center
  #安全认证的登录账号
  security:
    user:
      password: 123456
      name: admin
server:
  port: 8889
eureka:
  client:
    #注册中心不需要将自己当作客户端注册
    register-with-eureka: false
    #注册中心的职责是维护服务的信息等,不需要去检索相关的服务
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:${server.port}/eureka/
    #关闭自我保护模式
  server:
    enable-self-preservation: false

(3)权限类配置bean如下:

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
//开启权限认证
@EnableWebSecurity
public class SercurityConfig  extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭csrf
        http.csrf().disable();
        //任何请求都需要进行权限的校验包括基本请求
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

(4)启动类

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceMain {
    public static void main(String agrs[]){
        SpringApplication.run(EurekaServiceMain.class,agrs);
    }
}

3.服务提供者

(1)pom.xml:



    
        spring boot-demon2
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    eureka-instance
    

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-web
        
    

(2)配置文件如下:

server:
  port: 8085
spring:
  application:
    name: serviceProvider

eureka:
  client:
    service-url:
      defaultZone: http://admin:123456@localhost:8889/eureka/
  instance:
    #采用ip注册
    prefer-ip-address: true
    #定义实列ID格式
    #${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

(3)对外提供接口:

package com.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloHandler {
    @GetMapping("/hello")
    public String getMessage(){
        return "你好,世界!";
    }
}

(4)启动类:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class eurekaServiceProvider {
    public static void main(String[] args) {
        SpringApplication.run(eurekaServiceProvider.class,args);
    }
}

4.消费者

(1)pom.xml配置如下:



    
        spring boot-demon2
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    eurekaClient
    

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    

(2)配置文件(application.yml)

server:
  port: 8881
spring:
  application:
    name: client
eureka:
  client:
    service-url:
      defaultZone: http://admin:123456@localhost:8889/eureka/

  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

(3)RestTemplate配置:

package com.Config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class BeanConfig {
    @Bean
    //负载均衡
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

(4)服务调用

package com.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class helloService {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/client/hello")
    public String getString(){
//        return restTemplate.getForObject("http://localhost:8085/hello", String.class);
        return  restTemplate.getForObject("http://serviceProvider/hello", String.class);
    }
}

(5)启动类

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ClientMain {
    public static void main(String[] args) {
        SpringApplication.run(ClientMain.class,args);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1039935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号