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();
    }
}
相关推荐
AAA修煤气灶刘哥21 分钟前
Java+AI 驱动的体检报告智能解析:从 PDF 提取到数据落地全指南
java·人工智能·后端
wxy31931 分钟前
嵌入式LINUX——————TCP并发服务器
java·linux·网络
★YUI★1 小时前
学习游戏制作记录(玩家掉落系统,删除物品功能和独特物品)8.17
java·学习·游戏·unity·c#
微小的xx1 小时前
java + html 图片点击文字验证码
java·python·html
mask哥1 小时前
详解flink java基础(一)
java·大数据·微服务·flink·实时计算·领域驱动
克拉克盖博1 小时前
chapter03_Bean的实例化与策略模式
java·spring·策略模式
创码小奇客2 小时前
架构师私藏:SpringBoot 集成 Hera,让日志查看从 “找罪证” 变 “查答案”
spring boot·spring cloud·trae
DashVector2 小时前
如何通过Java SDK分组检索Doc
java·数据库·面试
程序员清风2 小时前
跳表的原理和时间复杂度,为什么还需要字典结构配合?
java·后端·面试
渣哥2 小时前
Kafka消息丢失的3种场景,生产环境千万要注意
java