温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行Eureka编写服务

发布时间:2021-09-29 14:32:35 来源:亿速云 阅读:108 作者:柒染 栏目:编程语言

如何进行Eureka编写服务,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1)创建项目注册到 Eureka

注册中心已经创建并且启动好了,接下来我们实现将一个服务提供者 eureka-client-user-service 注册到 Eureka 中,并提供一个接口给其他服务调用。

首先还是创建一个 Maven 项目,然后在 pom.xml 中增加相关依赖,代码如下所示。

<!-- Spring Boot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version><relativePath /></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- eureka --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies><!-- Spring Cloud --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

创建一个启动类 App,代码如下所示。

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

启动类的方法与之前没有多大区别,只是注解换成 @EnableDiscoveryClient,表示当前服务是一个 Eureka 的客户端。

接下来在 src/main/resources 下面创建一个 application.properties 属性文件,增加下面的配置:

spring.application.name= eureka-client-user-serviceserver.port=8081eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/# 采用IP注册eureka.instance.preferIpAddress=true# 定义实例ID格式eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

eureka.client.serviceUrl.defaultZone 的地址就是我们之前启动的 Eureka 服务的地址,在启动的时候需要将自身的信息注册到 Eureka 中去。

执行 App 启动服务,我们可以看到控制台中有输出注册信息的日志:

DiscoveryClient_EUREKA-CLIENT-USER-SERVICE/eureka-client-user-service:192.168.31.245:8081 - registration status: 204

我们可以进一步检查服务是否注册成功。回到之前打开的 Eureka 的 Web 控制台,刷新页面,就可以看到新注册的服务信息了。

2)编写提供接口

创建一个 Controller,提供一个接口给其他服务查询,代码如下所示。

@RestControllerpublic class UserController {@GetMapping("/user/hello")public String hello() {return “hello”;
    }
}

重启服务,访问 http://localhost:8081/user/hello (http://localhost%EF%BC%9A8081/user/hello),如果能看到我们返回的 Hello 字符串,就证明接口提供成功了。

关于如何进行Eureka编写服务问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI