Eureka搭建

1.注册中心server端
1.1.引入依赖
复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2.配置
复制代码
server:
  port: 8761
spring:
  application:
    name: eureka-server #服务名称,如多台server,那么保持一致
  security: #security用户名密码
    user:
      name: root
      password: root
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true #设置显示ip地址
    instance-id: ${spring.cloud.client.ipaddress}:${server.port}
  client:
    fetch-registry: false #是否注册自己默认为true,如果
    register-with-eureka: false
    service-url: #注册中心地址  root:root@ 为security用户名密码
      defaultZone: http://root:root@localhost:8761/eureka/ #http://root:root@${eureka.instance.hostname}:${server.port}/eureka/
1.3.server端security配置
复制代码
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter  {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        //解决/eureka下的跨域问题
        http.csrf().ignoringAntMatchers("/eureka/**");
    }

}
1.4.启动类加上
复制代码
@EnableEurekaServer
2.provider服务提供方
2.1.client端依赖
复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.2.client配置
复制代码
server:
  port: 7070

spring:
  application:
    name: service-provider #服务名称,多来provider服务名保持一致

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipaddress}:${server.port}
  client:
    service-url:	
      defaultZone: http://root:root@localhost:8761/eureka/ #,http://localhost:8762/eureka/ 注册中心url多个注册中心用","分隔

service-provider: #ribbon配置 service-provider为服务名
  ribbon: #RandomRule为轮询
    NFLoadBlanceRuleClassName: com.netflix.loadblancer.RandomRule
2.3.提供服务接口
复制代码
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @GetMapping("/getUserByName/{name}")
    public User getUserByName(@PathVariable String name){
        log.info("name={}",name);
        return new User("王渝",22);
    }
}
2.4.开启client
复制代码
@EnableEurekaClient //可以不加
3.consumer服务使用方
3.1.依赖
复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.2.配置
复制代码
server:
  port: 9090

spring:
  application:
    name: service-consumer

eureka:
  client:
    service-url: #注册中心向外暴露的地址
      defaultZone: http://root:root@localhost:8761/eureka/ #,http://localhost:8762/eureka/  多台server已逗号分隔
    register-with-eureka: false
    registry-fetch-interval-seconds: 10 
    #表示EurekaClient间隔多久去拉去注册信息 默认30秒
3.3.Controller接口
复制代码
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceFeign userServiceFeign;

    @GetMapping("/name2/{name}")
    public User getUserByName2(@PathVariable String name){
        return  userServiceFeign.getUserName(name);
    }
}
3.4.Service接口(方法一 feign调用)
复制代码
//service-provide为服务提供方的名称
@FeignClient(value = "service-provider") 
public interface UserServiceFeign {
    
	// /user/getUserByName/{name} 为消息提供方的url
    @GetMapping("/user/getUserByName/{name}") 
    User getUserName(@PathVariable(value = "name") String name);


}
3.5Service接口(方法二 RestTemplate)
复制代码
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public User getUserByName(String name) {
        ResponseEntity<User> response = restTemplate.exchange("http://service-provider/user/getUserByName/" + name,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<User>() {
                });

        return response.getBody();
    }
}
相关推荐
jack_xu1 小时前
经典大厂面试题——缓存穿透、缓存击穿、缓存雪崩
java·redis·后端
CHQIUU2 小时前
Java 设计模式心法之第4篇 - 单例 (Singleton) 的正确打开方式与避坑指南
java·单例模式·设计模式
碎梦归途2 小时前
23种设计模式-结构型模式之享元模式(Java版本)
java·开发语言·jvm·设计模式·享元模式
幽络源小助理3 小时前
SpringBoot民宿管理系统开发实现
java·spring boot·springboot·民宿系统
东阳马生架构3 小时前
Nacos简介—1.Nacos使用简介
java
爱发飙的蜗牛3 小时前
springboot--web开发请求参数接收注解
java·spring boot·后端
码熔burning3 小时前
【MQ篇】RabbitMQ之工作队列模式!
java·分布式·rabbitmq·mq
优雅的落幕3 小时前
Spring--统一数据返回格式与统一异常处理
java·spring·状态模式
BillKu3 小时前
Spring Boot + MyBatis 动态字段更新方法
java·spring boot·mybatis