美文网首页
Spring Cloud整合Actuator及入门案例

Spring Cloud整合Actuator及入门案例

作者: Liuzhoulin | 来源:发表于2020-08-15 14:53 被阅读0次

一、编写服务提供者

   1) 添加相关依赖如下:

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-jpa</artifactId>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

        </dependency>

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>druid</artifactId>

            <version>1.1.20</version>

        </dependency>

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-actuator</artifactId>

        </dependency>

        <dependencyManagement>

            <dependencies>

                <dependency>

                    <groupId>org.springframework.cloud</groupId>

                    <artifactId>spring-cloud-dependencies</artifactId>

                    <version>Camden.SR4</version>

                    <type>pom</type>

                    <scope>import</scope>

                </dependency>

            </dependencies>

        </dependencyManagement>

    2) 创建表及插入数据如下:

        DROP TABLE IF EXISTS user;

        CREATE TABLE user(

            id BIGINT auto_increment,

            username VARCHAR(40),

            name VARCHAR(20),

            age INT(3),

            balance DECIMAL(10,2),

            PRIMARY KEY(id)

        )ENGINE=INNODB  DEFAULT CHARSET=utf8;

        INSERT INTO user(id,username,name,age,balance)VALUES(1,'account1','张三',20,100.00);

        INSERT INTO user(id,username,name,age,balance)VALUES(2,'account2','李四',28,180.00);

        INSERT INTO user(id,username,name,age,balance)VALUES(3,'account3','王五',32,280.00);

    3)创建对应的entity、dao、controller如下:

        entity:

下面是一些set、get方法

        dao:

案例模板,直接继承JPA的一些方法操作

        controller:

写个简单的查询,展示对应结果

    4)配置配置文件application.yml如下:

详细配置如上图所示

    5)启动项目,查询对应数据结果如下:

    

查询id为1的所有信息

    6)项目启动后,可访问actuator端口查看信息,结果如下:

http://localhost:8000/norman/health http://localhost:8000/norman/info

二、编写服务消费者

    1)添加依赖如服务提供者所示

    2)添加服务提供者的entity,在启动类中添加如下代码

        @Bean

        public RestTemplate restTemplate() {

            return new RestTemplate();

        }

    3)修改application.yml中的端口号,改成:8010,并创建controller如下:

访问 http://127.0.0.1:8010/user/1 ,结果如下图所示 一定要是在服务提供者项目启动的情况下访问

相关文章

网友评论

      本文标题:Spring Cloud整合Actuator及入门案例

      本文链接:https://www.haomeiwen.com/subject/ixaodktx.html